<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>显示所有照顾计划</title>
    <link rel="stylesheet" href="css/bootstrap.css">
    <script src="js/jquery.js" type="text/javascript"></script>
    <script src="js/bootstrap.js" type="text/javascript"></script>
    <script src="js/jsrender.js" type="text/javascript"></script>
    <script src="js/common.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function() {
            function showAll(){
                //请求所有部门信息
                if(localStorage.getItem("position")==1) {
                    $.ajax({
                        url: GC.bctx + "care/list",//后端请求映射路径
                        type: "GET",
                        headers: {"Authorization": localStorage.getItem("token")},
                        success: function (r) {
                            if (r.successed) {
                                //jsrender模板渲染
                                $("#cares").html($("#caresTemplate").render(r.data))
                            } else {
                                window.location.href = "login.html"
                            }
                        }
                    })
                }
                else{
                    $("#nurseIDLabel").prop("class","d-none")
                    $("#nurseNameLabel").prop("class","d-none")
                    $("#operation").prop("class","d-none")
                    $("#newCare").prop("class","d-none")
                    $.ajax({
                        url: GC.bctx + "care/nurseList/"+localStorage.getItem("account"),//后端请求映射路径
                        type: "GET",
                        headers: {"Authorization": localStorage.getItem("token")},
                        success: function (r) {
                            if (r.successed) {
                                //jsrender模板渲染
                                $("#cares").html($("#caresNurseTemplate").render(r.data))
                            } else {
                                window.location.href = "login.html"
                            }
                        }
                    })
                }
            }
            showAll()

            $("form").submit(function(){
                if($("#customerid").val()==""&&$("#customername").val()==""&&$("#nurseid").val()==""&&$("#nursename").val()==""){
                    // alert("查询全为空")
                    showAll()
                    return false;
                }
                if($("#customerid").val()!=""&&isNaN($("#customerid").val())){
                    alert("客户编号必须为纯数字!")
                    return false;
                }
                if($("#nurseid").val()!=""&&isNaN($("#nurseid").val())){
                    alert("护士编号必须为纯数字!")
                    return false;
                }
                if (localStorage.getItem("position")==1) {
                    $.ajax({
                        url: GC.bctx + "care/find",
                        type: "GET",
                        data: $("form").serialize(),//需要配上属性name：name="id" name="type"
                        headers: {"Authorization": localStorage.getItem("token")},
                        success: function (r) {
                            if (r.successed) {
                                //成功了渲染
                                $("#cares").html($("#caresTemplate").render(r.data))
                            } else {
                                window.location.href = "login.html"
                            }
                        }
                    })
                }
                else{
                    $.ajax({
                        url: GC.bctx + "care/nurseFind/"+localStorage.getItem("account"),
                        type: "GET",
                        data: $("form").serialize(),//需要配上属性name：name="id" name="type"
                        headers: {"Authorization": localStorage.getItem("token")},
                        success: function (r) {
                            if (r.successed) {
                                //成功了渲染
                                $("#cares").html($("#caresNurseTemplate").render(r.data))
                            } else {
                                window.location.href = "login.html"
                            }
                        }
                    })
                }
                //阻止表单正常提交
                return false;
            })

            $("#newCare").click(function() {
                window.location.href="insertCare.html"
            })


            $("#cares").on("click",".btn-delete",function () {
                //var id=$(this).parent().parent().find("td:first").text()   //传统方法
                var id = $(this).attr("data-id")            //取值方法
                //删除2：二次确认
                if(!window.confirm("确定删除以下客户编号的康复登记吗？ \n" + id)){
                    return;
                }
                if(!window.confirm("删除后数据无法恢复，再次确认")){
                    return;
                }
                //ajax提交请求
                $.ajax({
                    url:GC.bctx + "care/delete/"+id,
                    type:"DELETE",
                    headers: {"Authorization": localStorage.getItem("token")},
                    success:function(r){
                        if(r.successed){
                            //成功后
                            alert(r.message)
                            showAll()
                        }else{
                            alert(r.message)
                        }
                    }
                })
            })



        })
    </script>
</head>
<body>
<form  action="find" method="get" class="container-fluid text-center">
    <label>客户编号: <input id="customerid" name="customerid" type="text"></label>
    <label>客户名: <input id="customername" name="customername" type="text"></label>
    <label id="nurseIDLabel">护士编号: <input id="nurseid" name="nurseid"  type="text"></label>
    <label id="nurseNameLabel">护士名: <input id="nursename" name="nursename"  type="text"></label>
    <input type="submit" value="查 询">
    <input type="reset" value="重 置">
</form>
<button id="newCare" href="#" class="btn btn-primary">新增照顾信息</button>

<script id="caresTemplate" type="text/x-jsrender">
	<tr align="center">
		<td>{{:customerid}}</td>
		<td>{{:customername}}</td>
		<td>{{:nurseid}}</td>
		<td>{{:nursename}}</td>
		<td colspan="2" align="center">
            <a href="updateCare.html?id={{:customerid}}" class="btn btn-update btn-info" data-id="{{:customerid}}">修改</a>
            <button name="deleteCare" class="btn btn-delete btn-danger" data-id="{{:customerid}}">删除</button>
         </td>
	</tr>
    </script>

<script id="caresNurseTemplate" type="text/x-jsrender">
	<tr align="center">
		<td>{{:customerid}}</td>
		<td>{{:customername}}</td>
		<td>{{:nurseid}}</td>
		<td>{{:nursename}}</td>
		<td class="d-none" colspan="2" align="center">
            <a href="updateCare.html?id={{:customerid}}" class="btn btn-update btn-info" data-id="{{:customerid}}">修改</a>
            <button name="deleteCare" class="btn btn-delete btn-danger" data-id="{{:customerid}}">删除</button>
         </td>
	</tr>
    </script>

<table class="table table-bordered table-hover">
    <tr class="text-center">
        <th>客户编号</th>
        <th>客户名</th>
        <th>护士编号</th>
        <th>护士名</th>
        <th id="operation">操作</th>
    </tr>
    <tbody id="cares"></tbody>
</table>

</body>
</html>